<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * The project “dev”
 * File name “Setting_model.php”
 *
 * User: znx
 * Company: 思议创想
 * Q  Q: 865303386
 * Site: www.siyi360.com
 *
 * Date: 2017-03-06
 * Time: 18:14
 */
class Setting_model extends CI_Model {
    /*
     * setting_get
     * 获取系统配置数据
     *
     * @access public
     * @param string $field需要获取的系统配置字段（option，通过此字段查询配置项的值）
     * */
    public function setting_get($field)
    {
        $this->db->where('config_option', $field);
        $query = $this->db->get('config');
        $result = @$query->result_array()[0]['config_value'];

        return $result;
    }

    /*
     * setting_update
     * 系统配置更新
     *
     * @access public
     * */
    public function setting_update()
    {
        //遍历post数据
        foreach ($_POST as $key=>$val) {
            //查询配置项是否存在，不存在则insert，存在则update
            $count = $this->db->from('config')
                ->where('config_option', $key)
                ->count_all_results();
            if ($count>=1) {
                $this->db->where('config_option',$key);
                $req = $this->db->update('config', array('config_value'=>$val));
            } else {
                $req = $this->db->insert('config', array('config_option'=>$key,'config_value'=>$val));
            }
        }

        if ($req) {
            $result = array('status'=>1, 'msg'=>'操作成功！', 'result'=>'');
        } else {
            $result = array('status'=>0, 'msg'=>'操作失败！', 'result'=>'');
        }
        return $result;
    }
}